import Vue from 'vue'
import { Toast } from 'mint-ui';
import baseurl from '@/const/baseurl.js'
var config = {
    url: '/auth/getguid',
    method: 'post',
    baseurl: baseurl,
    headers: { 'authorization': 'XMLHttpRequest' }
}
var param = function(obj) {
    var query = '',
        name, value, fullSubName, subName, subValue, innerObj, i;
    for (name in obj) {
        value = obj[name];
        if (value instanceof Array) {
            for (i = 0; i < value.length; ++i) {
                subValue = value[i];
                fullSubName = name + '[' + i + ']';
                innerObj = {};
                innerObj[fullSubName] = subValue;
                query += param(innerObj) + '&';
            }
        } else if (value instanceof Object) {
            for (subName in value) {
                subValue = value[subName];
                fullSubName = name + '[' + subName + ']';
                innerObj = {};
                innerObj[fullSubName] = subValue;
                query += param(innerObj) + '&';
            }
        } else if (value !== undefined && value !== null)
            query += encodeURIComponent(name) + '=' + encodeURIComponent(value) + '&';
    }
    return query.length ? query.substr(0, query.length - 1) : query;
};
var httphelper = function(url, data) {
    console.log(Vue.prototype)
    var promise = new Promise(function(resolve, reject) {
        var http = Vue.prototype.$http
        data = data instanceof Object && String(data) !== '[object File]' ? param(data) : data;
        console.log('data=' + data);
        http.post(url, data, config)
            .then(function(res) {
                console.log(res.data)

                if (res.data == undefined || !res.data.ResponseResult || res.data.ResponseResult == undefined) {
                    let instance = Toast('出现错误');
                    setTimeout(() => {
                        instance.close();
                    }, 2000);
                }

                if (res.data.ResponseResult == 4) {
                    let instance = Toast(res.data.Msg);
                    setTimeout(() => {
                        instance.close();
                    }, 2000);
                }


                resolve(res.data)
            })
            .catch(function(err) {
                console.log(err)
                reject(err)
            })
    });
    return promise

}
export default httphelper